package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.RedisNewsConst;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.service.SeckillOrderServiceI;
import com.dongdongshop.util.IdWorker;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SeckillOrderServiceImpl implements SeckillOrderServiceI {

    @Resource
    private TbSeckillOrderMapper seckillOrderMapper;

    @Resource
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private IdWorker idWorker;



    @Override
    public void submitOrder(Long seckillId, Long userId) {

        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setValueSerializer(redisSerializer);
        //从缓存中获取秒杀的商品
        Integer goodsStockCount = Integer.parseInt(redisTemplate.boundValueOps(seckillId).get().toString()) ;
        if(null == goodsStockCount){
            //抛出异常，告诉调用方，我这边需要路慕
            throw new RuntimeException("商品不存在");
        }
        //商品库存为0
        if(goodsStockCount.intValue() <= 0){
            throw new RuntimeException("商品已售馨");
        }
        //扣减库存
        Long stockCount = redisTemplate.boundValueOps(seckillId).decrement();
        //同步缓存
        TbSeckillGoods goods = (TbSeckillGoods)redisTemplate.boundHashOps(RedisNewsConst.seckillList).get(seckillId);
        goods.setStockCount(stockCount.intValue());
        redisTemplate.boundHashOps(RedisNewsConst.seckillList).put(seckillId,goods);

        //如果已经是最后一件商品
        if (stockCount == 0){//商品被抢光
            TbSeckillGoods seckillGoods = (TbSeckillGoods)redisTemplate.boundHashOps(RedisNewsConst.seckillList).get(seckillId);
            seckillGoods.setStockCount(0);
            //同步数据库
            tbSeckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
            //如果卖完了，删除缓存
            redisTemplate.boundHashOps(RedisNewsConst.seckillList).delete(seckillId);
            //删除库存
            redisTemplate.delete(seckillId);
        }


        //保存订单
        Map<String,Long> msg = new HashMap<>();
        msg.put("seckillId",seckillId);
        msg.put("userId",userId);
        rocketMQTemplate.convertAndSend(RedisNewsConst.SECKILLGOODS,msg);

        //保存订单

/*
        TbSeckillOrder seckillOrder = new TbSeckillOrder();
        long orderId = idWorker.nextId();
        seckillOrder.setId(orderId);
        seckillOrder.setCreateTime(new Date());
        seckillOrder.setMoney(seckillGoods.getCostPrice());//秒杀价格，支付的价格
        seckillOrder.setSeckillId(seckillId);
        seckillOrder.setSellerId(seckillGoods.getSellerId());
        seckillOrder.setStatus("0");
        seckillOrder.setUserId(userId+"");
        redisTemplate.boundHashOps(RedisNewsConst.seckillOrder).put(userId,seckillOrder);*/
    }

    @Override
    public TbSeckillOrder findOrder(Long seckillId, Long id) {
        return  (TbSeckillOrder)redisTemplate.boundHashOps(RedisNewsConst.seckillList).get(id+"_"+seckillId);
    }
}
